<?php
namespace App\Api\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;

// 授权
class AuthController
{
    /**
     * 用户登陆
     * @unauthenticated
     */
    public function login(Request $request){
        $request->validate(
            [
                'username' => 'required',
                'password' => 'required',
            ], [
                'username.required' => '请填写用户名',
                'password.required' => '请填写密码',
            ]
        );

        $username = $request->get('username');
        $password = $request->get('password');

        $MemberUserModel = '\App\Models\MemberUser';

        $center = $MemberUserModel::where(['username'=>$username])->first(); // 'is_active'=>1

        if(empty($center)){
            return response()->json([
                'code'  => 500,
                'msg'   => '账户信息不存在'
            ]);
        }
        if(!Hash::check($password, $center->password)){
            return [
                'code'  => 500,
                'msg'   => '账户密码不正确'
            ];
        }

        if (!$token = auth('adminapi')->fromUser($center)) {
            return response()->json([
                'code'  => 500,
                'msg'   => '登录失败',
            ]);
        }
        $data = [
            'access_token'  => $token,
            'token_type'    => 'bearer',
            'expires_in'    => auth('memberapi')->factory()->getTTL() * 60 * 24 * 7,
            'username'      => $center->username,
        ];
        return response()->json(['code'=>0,'msg'=>'ok','data'=>$data]);
    }

    /**
     * 退出登陆
     * @unauthenticated
     */
    public function logout(Request $request){
        auth('memberapi')->logout();
        return response()->json([
            'code'  => 0,
            'msg'   => '退出成功'
        ]);
    }


}
